package com.woniu.controller;

import com.alibaba.excel.EasyExcel;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Doctor;
import com.woniu.pojo.Package;
import com.woniu.pojo.Team;
import com.woniu.service.ITeamService;
import com.woniu.vo.QuerySearchTeamVo;
import com.woniu.vo.QueryUserVo;
import com.woniu.vo.TeamLabelVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author rsw
 * @since 2022-10-11
 */
@RestController
@Api(tags = "团队模块")
@RequestMapping("/team")
    public class TeamController extends BaseController {

    @Autowired
    private ITeamService teamService;

    @GetMapping
    @ApiOperation("查询所有团队")
    public Result searchAll() {
        List<Team> listTeam = teamService.list();

        return toDataResult(listTeam, "查询成功");

    }

    @ApiOperation("新增团队")
    @PostMapping
    public Result add(@RequestBody Team team) {
        boolean flag = teamService.save(team);
        return toResult(flag);

    }

    @ApiOperation("删除团队")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        Long ids = (long) id;
        boolean flag = teamService.removeById(ids);
        return toResult(flag);
    }

    @ApiOperation("根据id查询团队")
    @GetMapping("/{id}")
    public Result modify(@PathVariable int id) {
        Team byId = teamService.getById(id);
        return toDataResult(byId);
    }

    @ApiOperation("根据id修改团队")
    @PutMapping
    public Result modify(@RequestBody Team team) {
        boolean flag = teamService.updateById(team);
        return toResult(flag);
    }


    /**
     * 分页搜索查找所有医生
     */
    @ApiOperation("分页搜索团队")
    @PostMapping("/search/{currentPage}/{pageSize}")
    public Result searchByPage(@PathVariable String currentPage, @PathVariable String pageSize,
                               @RequestBody QuerySearchTeamVo searchTeamVo) {
        PageResult pageResult = teamService.searchByPage(currentPage, pageSize, searchTeamVo);
        return toDataResult(pageResult);
    }
    @ApiOperation("导出")
    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        List<Team> teamList = teamService.list();
        System.out.println(teamList+"ssssssssssssssssssssfakfgakfgkasjhgfkasgfigsaikfugsaikfgiujsa");

        EasyExcel.write(response.getOutputStream(), Team.class).sheet("导出").doWrite(teamList);
    }

    @ApiOperation("根据团队id查找所有服务包")
    @GetMapping("/team/package/{id}")
    public Result searchPackageByTeamId(@PathVariable int id){
        List<QueryUserVo> pgList = teamService.getPgByteamId(id);
        return toDataResult(pgList);
    }

    @ApiOperation("根据团队id查找标签")
    @GetMapping("/team/label/{id}")
    public Result searchLabelByTeamId(@PathVariable int id){
        List<String> labelList = teamService.getLabelByteamId(id);
        return toDataResult(labelList);
    }

    @ApiOperation("根据团队id查找所有医生")
    @GetMapping("/team/doctor/{id}")
    public Result searchDoctorByTeamId(@PathVariable Long id){
        List<Doctor> doctorList = teamService.searchDoctorByTeamId(id);
        return toDataResult(doctorList);
    }

    @ApiOperation("根据团队id查找所有服务包2")
    @GetMapping("/team/package2/{id}")
    public Result searchPackageByTeamId2(@PathVariable Long id){
        List<Package> pgList = teamService.getPgByteamId2(id);
        return toDataResult(pgList);
    }

}
